package com.itheima.mapper;

import com.itheima.pojo.Order;
import com.itheima.pojo.OrderVO;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealCount;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author Encounter
 * @date 2024/10/10 12:49<br/>
 */
@Mapper
public interface OrderMapper
    {
        /**
         * 动态分页查询预约信息
         *
         * @param queryMap 查询地图
         * @return {@link Page }<{@link Order }>
         */
        Page<OrderVO> query(Map<String, Object> queryMap);
        
        /**
         * 修改order
         *
         * @param order 次序
         * @return int
         */
        int update(Order order);
        
        /**
         * 查询 SetMeal 计数
         *
         * @return {@link List }<{@link SetmealCount }>
         */
        List<SetmealCount> querySetmealCount();
        
        /**
         * 获取 Today 访问次数
         *
         * @param today 今天
         * @return {@link Integer }
         */
        @Select("select count(*) from t_order where orderDate = #{today} and orderStatus = '已到诊'")
        Integer getTodayVisitsNumber(String today);
        
        /**
         * 获取本周访问次数
         *
         * @param begin 开始
         * @param end   结束
         * @return {@link Integer }
         */
        @Select("select count(*) from t_order " +
                "where orderDate >= #{begin} and orderDate <= #{end} and orderStatus = '已到诊'")
        Integer getVisitsNumber(String begin, String end);
        
        /**
         * 获取热餐
         *
         * @return {@link List }<{@link Setmeal }>
         */
        @Select("""
                with totalCount as (select count(*) total from t_order)
                select s.name, count(*) / (t.total) proportion,count(*) setmealCount
                from t_order o
                         left join t_setmeal s on o.setmeal_id = s.id,
                     totalCount t
                group by setmeal_id, t.total
                order by proportion desc
                limit 4""")
        List<Map<String, Object>> getHotSetmeal();
        
        /**
         * 获取所有计数
         *
         * @return {@link Integer }
         */
        @Select("select count(*) from t_order")
        Integer getAllCount();
        
        /**
         * 获取订单号
         *
         * @param begin 开始
         * @param end   结束
         * @return {@link Integer }
         */
        @Select("select count(*) from t_order where orderDate >= #{begin} and orderDate <= #{end}")
        Integer getOrderNumber(String begin, String end);
    }
